
import AliyunOSS from 'react-native-aliyun-oss-cp';

import Config from '../config';
import BaseModel from './BaseModel';



export default class AliModel  extends BaseModel{
    constructor() {
    super();
    this.config = {
      isScuess:false,
      lastTime:0, //最后请求时间
      ossConfig:{}
    };

    var self = this;
    this.uploadFile = this.uploadFile.bind(this);
    this.checkOssTokenAndCallBack = this.checkOssTokenAndCallBack.bind(this);
  }

   uploadConent(key, content,onScuess,onError) {
    // 先检查token再回调
    // this.checkOssTokenAndCallBack(function(){
      // 回调了，初始化陪你
    //   var uploadConfig = {
    //     bucketName: this.config.ossConfig.bucket_name,
    //     sourceFile: new Buffer(content), // local file path
    //     ossFile: this.config.ossConfig.key_prefix + key
    //   };
    //    var uploadProgress = p => console.log(p.currentSize / p.totalSize);
    //    AliyunOSS.uploadObjectAsync(uploadConfig)
    //       .then((resp) => {
    //         //上传成功
    //          console.log(resp);
    //          AliyunOSS.removeEventListener('uploadProgress', uploadProgress);
    //          onScuess(resp);
    //    }).catch((err)=>{
    //     onError(err);
    //      console.log(err);
    //      });
    // },onError);
  }

  uploadFile(key,file,onScuess,scuessCallThis,onError) {
    console.log("uploadFile", key, file);
    var self = this;
    file = file.replace("file://","");
    //"file:///Users/demon/Library/Developer/CoreSimulator/Devices/A982EAF9-5EDA-4E79-A20E-6AF4C937D06D/data/Containers/Data/Application/F9C7F855-99A8-4CA8-AE1D-0337824111AE/Documents/4.jpeg"
    // 先检查token再回调
    this.checkOssTokenAndCallBack(function(){

    // 回调了，初始化配置
    var uploadConfig = {
      bucketName: self.config.ossConfig.bucket_name,
      sourceFile: file, // local file path
      ossFile: self.config.ossConfig.key_prefix + key,
    };
     // var uploadProgress = p => console.log(p.currentSize / p.totalSize);
     AliyunOSS.uploadObjectAsync(uploadConfig)
        .then((resp) => {
          //上传成功
           console.log(resp);
           // AliyunOSS.removeEventListener('uploadProgress', uploadProgress);
           onScuess.call(scuessCallThis,self.config.ossConfig.key_prefix,resp);
     }).catch((err)=>{
      if(onError){
        onError(err);
      }
       console.log(err);
       });
    },onError);
  }

 checkOssTokenAndCallBack(callback,onError){
  var curTime = new Date().getTime();
  if(this.config.isScuess && curTime < Config.aliInterval + this.config.lastTime){
    callback();
    return;
  }
  var selfThis = this;
   this.sendPostMustJson(Config.serverUrlHttps + "/app/media/credentials", JSON.stringify({}), function(json,isScuess){
    if( isScuess == false){
      console.log("请求sts出错");
      if(onError){
        onError(json);
      }
      return;
    }
    selfThis.config.ossConfig = json;
    selfThis.initOssConfig(selfThis);

    selfThis.config.isScuess = true;
    selfThis.config.lastTime = new Date().getTime();
    callback();
   });
 }


 initOssConfig(selfThis){
  AliyunOSS.enableOSSLog();
  var ossConfig ={
    AccessKey : selfThis.config.ossConfig.access_key_id,
    SecretKey : selfThis.config.ossConfig.access_key_secret,
    SecretToken : selfThis.config.ossConfig.security_token
  }
  Config.aliUrl = "http://"+selfThis.config.ossConfig.bucket_name+"."+selfThis.config.ossConfig.end_point+"/";
  var endPoint = selfThis.config.ossConfig.end_point;
  AliyunOSS.initWithKey(ossConfig, endPoint);
 }
}
